import { GameStage } from '../constant/GameStage';
import { SocketGameEvent } from '../constant/SocketGameEvent';
import { SocketRoomEvent } from '../constant/SocketRoomEvent';
import { IPlayerVo } from '../data/Player';
import { IPokerVo, Poker } from '../data/Poker';
import { IUserVo } from '../data/User';
import SocketManager from './SocketManager'

/**
 * 游戏相关事件通知者（Socket通知）
 */
export default class GameNotifier extends SocketManager {

  private static _instance: GameNotifier = null!

  public static get instance(): GameNotifier {
    if (this._instance == null) {
      this._instance = new GameNotifier()
    }
    return this._instance
  }

  private constructor() {
    super()
  }

  /**
   * 通知 有人进入房间
   * @param uid 进入的该房间的用户
   * @param index 房间中的位置
   * @param roomId 通知的房间
   */
  public sendJoinRoom(player: IPlayerVo, index: number, roomId: string,) {
    this.intoRoom(player.id, roomId)
    this.sendToRoom(player.id, roomId, {
      event: SocketRoomEvent.JOIN,
      data: {
        player,
        index
      }
    })
  }

  /**
   * 通知 有人离开房间
   * @param uid 
   * @param index 房间中的位置
   * @param roomId 
   */
  public sendLeaveRoom(uid: string, index: number, roomId: string) {
    this.leaveRoom(uid, roomId)
    this.sendToRoom(uid, roomId, {
      event: SocketRoomEvent.LEAVE,
      data: {
        uid,
        index
      }
    })
  }

  /**
   * 通知 有人因为掉线离开房间
   * @param index 
   * @param roomId 
   */
  public sendLeaveRoomByDisconnect(index: number, roomId: string) {
    this.sendToRoomAll(roomId, {
      event: SocketRoomEvent.LEAVE,
      data: {
        index
      }
    })
  }

  /**
   * 通知 指定房间的游戏状态被修改
   * @param roomId 
   * @param stage 
   */
  public sendChangeGameStage(roomId: string, stage: GameStage) {
    this.sendToRoomAll(roomId, {
      event: SocketGameEvent.CHANGE_STAGE,
      data: {
        stage
      }
    })
  }

  /**
   * 通知 有人准备
   * @param uid 
   * @param roomId 
   * @param isPrepare 准备/取消准备
   */
  public sendPrepare(uid: string, roomId: string, isPrepare: boolean) {
    this.sendToRoom(uid, roomId, {
      event: SocketGameEvent.PREPARE,
      data: {
        uid,
        isPrepare
      }
    })
  }

  /**
   * 通知 系统发牌
   * @param uid 目标用户
   * @param cards 目标用户的手牌
   */
  public sendDeal(uid: string, cards: Array<IPokerVo>) {
    this.send(uid, {
      event: SocketGameEvent.DEAL,
      data: {
        uid,
        cards
      }
    })
  }

  /**
   * 通知 轮到谁叫地主
   * @param uid 叫地主的玩家id
   * @param roomId 
   */
  public sendSetCallPlayer(uid: string, roomId: string) {
    this.sendToRoomAll(roomId, {
      event: SocketGameEvent.SET_CALL_PLAYER,
      data: {
        uid
      }
    })
  }

  /**
   * 通知 有人叫地主/不叫地主
   * @param uid 
   * @param isCall 
   */
  public sendCallLandlord(uid: string, roomId: string, isCall: boolean) {
    this.sendToRoomAll(roomId, {
      event: SocketGameEvent.CALL,
      data: {
        uid,
        isCall
      }
    })
  }

  /**
   * 通知 系统设置地主
   * @param uid 
   * @param roomId
   * @param cards 地主牌
   */
  public sendSetLandlord(uid: string, roomId: string, cards: Array<IPokerVo>) {
    this.sendToRoomAll(roomId, {
      event: SocketGameEvent.SET_LANDLORD,
      data: {
        uid,
        cards
      }
    })
  }

  /**
   * 通知 轮到谁出牌
   * @param uid 
   * @param roomId 
   */
  public sendSetPlay(uid: string, roomId: string) {
    this.sendToRoomAll(roomId, {
      event: SocketGameEvent.SET_PLAY,
      data: {
        uid
      }
    })
  }

  /**
   * 通知 有人出牌
   * @param uid 
   * @param roomId 
   * @param cards 
   */
  public sendPlayCards(uid: string, roomId: string, isPlay: boolean, cards: Array<IPokerVo>) {
    this.sendToRoom(uid, roomId, {
      event: SocketGameEvent.PLAY_CARDS,
      data: {
        uid,
        isPlay,
        cards
      }
    })
  }

  /**
   * 通知 游戏结果
   * @param roomId 
   * @param result 游戏结果数据
   */
  public sendResult(roomId: string, result: any) {
    this.sendToRoomAll(roomId, {
      event: SocketGameEvent.RESULT,
      data: {
        result
      }
    })
  }
}